/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.tnmk.worktime.service.model.Time;

/**
 *
 * @author tnmkhoi
 */
public class TimeUtils {

  public static final long ONE_HOUR_MILLIS = 3600000;
  public static final long ONE_SECOND_MILLIS = 1000;
//  public static final String DEFAULT_TIME_PATTERN = "hh:mm:ss a";
  public static final String DEFAULT_TIME_PATTERN = "hh:mm:ss a";
  public static final String DEFAULT_HOUR_PATTERN = "HH:mm:ss";
  public static final String DEFAULT_DATETIME_PATTERN = "dd/MM/yyyy hh:mm:ss a";
  public static final String DEFAULT_DATE_PATTERN = "dd/MM/yyyy";
  public static final String DEFAULT_DATE_WEEKDAY_PATTERN = "dd/MM/yyyy EEE";
  public static final String DEFAULT_DATETIME_WEEKDAY_PATTERN = "dd/MM/yyyy hh:mm:ss a (EEE)";
  public static final String DEFAULT_MONTH_PATTERN = "MM/yyyy";

  public static String toString(DateTime time, String pattern) {
    DateTimeFormatterBuilder fmtb = new DateTimeFormatterBuilder();
    fmtb.appendPattern(pattern);
    DateTimeFormatter fmt = fmtb.toFormatter();
    String rs = fmt.print(time);
    return rs;
  }

  public static DateTime toDateTime(String dateTimeString, String pattern) {
    try {
      DateTimeFormatterBuilder fmtb = new DateTimeFormatterBuilder();
      fmtb.appendPattern(pattern);
      DateTimeFormatter fmt = fmtb.toFormatter();
      DateTime rs = fmt.parseDateTime(dateTimeString);
      return rs;
    } catch (RuntimeException ex) {
      System.out.println("Error toDateTime("+dateTimeString+", "+pattern+")");
      throw ex;
    }
  }

  public static String toString(DateTime time) {
    return toString(time, DEFAULT_DATETIME_PATTERN);
  }

  public static String toHourString(DateTime time) {
    return toString(time, DEFAULT_HOUR_PATTERN);
  }

  public static String toTimeString(DateTime time) {
    return toString(time, DEFAULT_TIME_PATTERN);
  }

  public static String toTimeAndDecimalString(float hoursDecimal) {
    Time time = new Time(hoursDecimal);
    String s = NumUtils.toString(hoursDecimal) + " (" + time + ")";
    return s;
  }

  public static DateTime toDateTime(String dateTimeString) {
    return toDateTime(dateTimeString, DEFAULT_DATETIME_PATTERN);
  }

  public static DateTime toTime(String dateTimeString) {
    return toDateTime(dateTimeString, DEFAULT_TIME_PATTERN);
  }

//  public static Time toTime(float hoursDecimal) {
//    int hours = (int) hoursDecimal;
//    float minutesDecimal = ((hoursDecimal - hours) * 60);
//    int minutes = (int) minutesDecimal;
//    int seconds = (int) ((minutesDecimal - minutes) * 60);
//    Time rs = new Time(hours, minutes, seconds);
//    return rs;
//  }

  public static DateTime addHours(DateTime time, float hoursDecimal) {
    DateTime rs;
    int hours = (int) hoursDecimal;
    float minutesDecimal = ((hoursDecimal - hours) * 60);
    int minutes = (int) minutesDecimal;
    int seconds = (int) ((minutesDecimal - minutes) * 60);

    rs = time.plusHours(hours);
    rs = rs.plusMinutes(minutes);
    rs = rs.plusSeconds(seconds);
    return rs;
  }

  public static float calculatePeriod(DateTime begin, DateTime end) {
    long mills = end.getMillis() - begin.getMillis();
    float rs = (float) mills / ONE_HOUR_MILLIS;
    return rs;
  }

  public static boolean isSameDay(DateTime a, DateTime b) {
    boolean rs = (a.getYear() == b.getYear() && a.getDayOfYear() == b.getDayOfYear());
    return rs;
  }
}
